package com.travelplanning.admin.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travelplanning.admin.common.biz.user.UserContext;
import com.travelplanning.admin.common.exception.ClientException;

import com.travelplanning.admin.dao.entity.OrderDO;

import com.travelplanning.admin.dao.mapper.OrderMapeper;
import com.travelplanning.admin.dto.req.OrderAddReqDTO;
import com.travelplanning.admin.dto.resp.ContentQueryRespDTO;
import com.travelplanning.admin.dto.resp.OrderQueryRespDTO;

import com.travelplanning.admin.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 用户接口实现层
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapeper, OrderDO> implements OrderService {


    @Override
    public void addOrder(OrderAddReqDTO requestparam) {
        OrderDO orderDO = OrderDO.builder()
                .spotTitle(requestparam.getSpotTitle())
                .spotImg(requestparam.getSpotImg())
                .spotNum(requestparam.getSpotNum())
                .spotPrice(requestparam.getSpotPrice())
                .userId(UserContext.getUserId())
                .build();
        int insert = baseMapper.insert(orderDO);
        if (insert <= 0){
            throw new ClientException("新增订单失败");
        }
    }

    @Override
    public List<OrderQueryRespDTO> QueryOrder() {
        LambdaQueryWrapper<OrderDO> queryWrapper = Wrappers.lambdaQuery(OrderDO.class)
                .eq(OrderDO::getUserId, UserContext.getUserId());
        List<OrderDO> orderDOList = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(orderDOList)) {
            log.info("Content list is empty.");
            return Collections.emptyList(); // 返回空列表，不进行后续操作
        }

        //使用大模型优化
        List<OrderQueryRespDTO> OrderQueryRespDTOList = orderDOList.parallelStream() // 使用并行流提高效率
                .map(OrderDO -> {
                    OrderQueryRespDTO orderQueryRespDTO = new OrderQueryRespDTO();
                    // 假设BeanUtil是安全的，且已处理循环引用和深拷贝问题
                    BeanUtil.copyProperties(OrderDO, orderQueryRespDTO);
                    return orderQueryRespDTO;
                })
                .collect(Collectors.toList());
        return OrderQueryRespDTOList;
    }
}
